package com.cw.base.jeeyc.controller;

import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * TODO
 *
 * @author chenw
 * @date 2021/5/22
 */
@RestController
@RequestMapping("")
@Slf4j
public class ElasticSearchController {
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @RequestMapping("/createIndex")
    public void createIndex() throws IOException {
        CreateIndexRequest flower = new CreateIndexRequest("flower");
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices()
            .create(flower, RequestOptions.DEFAULT);
        log.info(createIndexResponse.toString());
    }
    @RequestMapping("/existIndex")
    public void existIndex() throws IOException {
        GetIndexRequest flower = new GetIndexRequest ("flower");
        Boolean getIndexResponse = restHighLevelClient.indices()
            .exists(flower,RequestOptions.DEFAULT);
        log.info(getIndexResponse.toString());
    }
    @RequestMapping("/deleteIndex")
    public void deleteIndex() throws IOException {
        DeleteIndexRequest flower = new DeleteIndexRequest ("flower");
        AcknowledgedResponse delete = restHighLevelClient.indices()
            .delete(flower,RequestOptions.DEFAULT);
        log.info(delete.toString());
    }
}
